package com.aaie.gru.config;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Autowired
    private MyRealm myRealm;

    @Bean("shirFilter")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") SecurityManager securityManager) {
        ShiroFilterFactoryBean shiro = new ShiroFilterFactoryBean();
        shiro.setLoginUrl("/unlogin");  //没有登陆的json返回
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        // 配置不会被拦截的链接 顺序判断
        filterChainDefinitionMap.put("/login", "anon");
        //放行静态资源
        filterChainDefinitionMap.put("/static/**", "anon");
        filterChainDefinitionMap.put("/js/**", "anon");
        Map<String, Filter> filters = new LinkedHashMap<String, Filter>();
        filters.put("authc", new CorsFilter());
        shiro.setFilters(filters);
        //放行Swagger2页面
        filterChainDefinitionMap.put("/swagger-ui.html#", "anon");
        filterChainDefinitionMap.put("/swagger-ui.html", "anon");
        filterChainDefinitionMap.put("/swagger-resources", "anon");
        filterChainDefinitionMap.put("/swagger-resources/configuration/security", "anon");
        filterChainDefinitionMap.put("/swagger-resources/configuration/ui", "anon");
        filterChainDefinitionMap.put("/v2/api-docs", "anon");
        filterChainDefinitionMap.put("/webjars/springfox-swagger-ui/**", "anon");
        // 配置被拦截的链接 顺序判断
        filterChainDefinitionMap.put("/**", "authc");
        shiro.setFilterChainDefinitionMap(filterChainDefinitionMap);
        shiro.setSecurityManager(securityManager);
        return shiro;
    }


//    @Bean("securityManager")
//    public SecurityManager securityManager(SessionDaoConfig sessionDaoConfig) {
//        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        securityManager.setRealm(myRealm);
//        // 自定义session管理 使用redis
//        SessionConfig sessionConfig = new SessionConfig();
//        sessionConfig.setSessionDAO(sessionDaoConfig);
//        //sessionConfig.setSessionDAO(new SessionDaoConfig());
//        securityManager.setSessionManager(sessionConfig);
//        // 自定义缓存实现 使用redis
//        //def.setCacheManager();
//        return securityManager;
//    }

    @Bean("securityManager")
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 设置realm.
        securityManager.setRealm(myRealm);
        SessionConfig sessionConfig = new SessionConfig();
        securityManager.setSessionManager(sessionConfig);
        return securityManager;
    }
}
//---------------------
//作者：myth_gy
//来源：CSDN
//原文：https://blog.csdn.net/myth_g/article/details/82113150
//版权声明：本文为博主原创文章，转载请附上博文链接！